// JavaScript Document//Delete item
//sess_pos : position of session product
function delete_item(id, sess_pos, product_image, pd_id){
	skinvip_css = document.getElementById('skinvip_css'+sess_pos).value;
	skinvip_js = document.getElementById('skinvip_js'+sess_pos).value;

	var anwser = confirm(lang_alert_delete);
	i = id.parentNode.parentNode.rowIndex;
	if(anwser){
		//Delete row of table product
		document.getElementById('table_postnews_product').deleteRow(i);
		//Ajax for process data
		$(document).ready(function(){
			$.ajax({
				 type:"POST",
				 url:site_root_domain+"/index.php?site=skinvip&act=add", 
				 data:"sess_pos="+sess_pos+"&skinvip_css="+skinvip_css+"&skinvip_js="+skinvip_js+"&id="+pd_id,
    		});	
		
		});
	}
}


//Get value edit & delete
//Edit = 1 : editpage, edit = 0 : addpage
function get_value(id, value, edit){
	
	//If product has existed in database update data in product table
	if(edit != 0 ){
		document.getElementById('product_edit_save_to_db').value = edit;	
	}
	
	skinvip_color = document.getElementById("skinvip_color"+value).value;
	
	$("#skinvip_color").css('background', skinvip_color).val(skinvip_color);
	$("._wColorPicker_buttonColor").css('background', skinvip_color).val(skinvip_color);
	
	
	skinvip_css = document.getElementById("skinvip_css"+value).value;
	skinvip_js = document.getElementById("skinvip_js"+value).value;
	
	document.getElementById('response_css').innerHTML = skinvip_css;
	document.getElementById('response_js').innerHTML = skinvip_js;
	
	document.getElementById('skinvip_color').value = skinvip_color;
	document.getElementById('skinvip_css_h').value = skinvip_css;
	document.getElementById('skinvip_js_h').value = skinvip_js;
	//Set mode edit
	document.getElementById('product_edit').value = value;
	
	
}

//Check empty input

function check_empty_product(id){
	var value =document.getElementById(id).value;
	return (!value) ? false:true;
}




function set_empty(){
	$("#status").html('');
	$("#skinvip_color").val('');
	$("#skinvip_css_h").val('');
	$("#skinvip_js_h").val('');
	
	$("#product_edit").val('');
	
	$("#status").html('');
	
	$("#skinvip_css_delete").val('');
	$("#skinvip_js_delete").val('');
	$("#product_edit_save_to_db").val('0');
	
	$("#response_css").html('');
	$("#response_js").html('');
	
	
}

$(document).ready(function(){

	
	
	/********** Function click button **********/
   $("#input_submit_skinvip").click(function(){
	   
	//Process and save data if edit product
	if(document.getElementById('product_edit').value != ''){
		i = document.getElementById('product_edit').value;
		
		
		//Save image when edit
		
		
		html_thumb = '<span id="product_thumb"><img align="left" src="'+site_parent_domain+'/uploads/product/thumbnail/'+$("#product_image_url").val()+'" /></span>';
		html_thumb += $("#product_name").val();
		
		//Add color name
		back_box = "<label style='border: solid 1px #CCCCCC; background-color:"+$("#skinvip_color").val()+";  align:left'>&nbsp;&nbsp;&nbsp;&nbsp;</label>";
		$("#td_skinvip_color"+i).html((back_box+$("#skinvip_color").val()));
		$("#td_skinvip_css"+i).html(($("#skinvip_css_h").val()));
		$("#td_skinvip_js"+i).html(($("#skinvip_js_h").val()));
		
		//Set hidden value 
		$("#skinvip_color"+i).val($("#skinvip_color").val());
		$("#skinvip_css"+i).val($("#skinvip_css_h").val());
		$("#skinvip_js"+i).val($("#skinvip_js_h").val());
		
		//Delete image when editing product, if select file delete past file 
		if($("#skinvip_css_delete").val() != ''){
			skinvip_css_delete = $("#skinvip_css"+i).val();
			$.ajax({
				 type:"POST",
				 url:site_root_domain+"/index.php?site=skinvip&act=process_delete_js_css&folder=css", 
				 data:"skinvip_css="+skinvip_css_delete,
			});	
		}
		
		//Delete image when editing product, if select file delete past file 
		if($("#skinvip_js_delete").val() != ''){
			skinvip_js_delete = $("#skinvip_js"+i).val();
			$.ajax({
				 type:"POST",
				 url:site_root_domain+"/index.php?site=skinvip&act=process_delete_js_css&folder=js", 
				 data:"skinvip_js="+skinvip_js_delete,
			});	
		}
		
		//Set new hidden for css
		$("#skinvip_css"+i).val($("#skinvip_css_h").val());
		$("#skinvip_js"+i).val($("#skinvip_js_h").val());
		
		
		
		//Update session
		//Respon Content, price, discount
		
			$.ajax({
			 type:"POST",
			 url:site_root_domain+"/index.php?site=skinvip&act=add", //goi toi file ajax.php
			 data:"skinvip_color="+$("#skinvip_color").val()+"&skinvip_css="+$("#skinvip_css_h").val()+"&skinvip_js="+$("#skinvip_js_h").val()+"&edit="+i,
			});
		//Set mode edit
		//Set empty input
		set_empty();
		return ;
	}
	
	//Check input
	$("#skinvip_color").click(function(){
		$("#skinvip_color").removeClass('border_error_postnews_product');
	});
	
	$("#skinvip_css").hover(function(){
		$("#skinvip_css").removeClass('border_error_postnews_product');
	});
	
	$("#skinvip_js").hover(function(){
		$("#skinvip_js").removeClass('border_error_postnews_product');
	});
	
	if( !check_empty_product('skinvip_color', document.getElementById('skinvip_color').value) ){
		$("#skinvip_color ").addClass('border_error_postnews_product');	
		return false;
	}
	
	/*if( !check_empty_product('skinvip_css_h', document.getElementById('skinvip_css_h').value) ){
		$("#skinvip_css").addClass('border_error_postnews_product');
		return false;
	}
	
	if( !check_empty_product('skinvip_js_h', document.getElementById('skinvip_js_h').value) ){
		$("#skinvip_js").addClass('border_error_postnews_product');
		return false;
	}*/
	
	//Respon Content, price, discount
	
	$.ajax({
     type:"POST",
     url:site_root_domain+"/index.php?site=skinvip&act=add", //goi toi file ajax.php
     data:"skinvip_color="+$("#skinvip_color").val()+"&skinvip_css="+$("#skinvip_css_h").val()+"&skinvip_js="+$("#skinvip_js_h").val(),

     success:function(html){
      $(".block_postnews_product table").append(html);
     }
    });
	
	
	
	
	//Set empty input
	set_empty();
		
	
  });
  /*-------------------------------------------------*/
  
});
	
 
   
//Upload css

$(function(){
		var btnUpload=$('#skinvip_css');
		var response_css=$('#response_css');
		
		new AjaxUpload(btnUpload, {
			action: site_root_domain+"/index.php?site=skinvip&act=add",
			name: 'file_upload_css',
			onSubmit: function(file, ext){
				 if (! (ext && /^(css)$/.test(ext))){ 
                    // extension is not allowed 
					$("#response_css").addClass('upload_error');
					response_css.text(lang_css_not_valid);
					return false;
				}
				response_css.text(postnews_product_uploading);
			},
			onComplete: function(file, response){
				
				$("#response_css").removeClass('upload_error');
				$("#response_css").html(response);
				$('#skinvip_css_h').val(response);
				$('#skinvip_css_delete').val('1');
				//On completion clear the status
				status.text(postnews_product_uploaded);
				//$('#respon_demo').html(response);
				
					
					//Delete odl image if no click save button
					if( $('#product_image_url').val() ){
						$.ajax({
							 type:"POST",
							 url:site_root_domain+"/index.php?site=postnews&act=process_postnews_product_delete", 
							 data:"product_image="+$('#product_image_url').val(),
						});	
					}
			}
		});
		
	});
	
	//Upload css

$(function(){
		var btnUpload=$('#skinvip_js');
		var response_js=$('#response_js');
		
		new AjaxUpload(btnUpload, {
			action: site_root_domain+"/index.php?site=skinvip&act=add",
			name: 'file_upload_js',
			onSubmit: function(file, ext){
				 if (! (ext && /^(js)$/.test(ext))){ 
                    // extension is not allowed 
					$("#response_js").addClass('upload_error');
					response_js.text(lang_js_not_valid);
					return false;
				}
				response_js.text(postnews_product_uploading);
			},
			onComplete: function(file, response){
				$("#response_js").removeClass('upload_error');
				$("#response_js").html(response);
				$('#skinvip_js_h').val(response);
				$('#skinvip_js_delete').val('1');
				//On completion clear the status
				status.text(postnews_product_uploaded);
				//$('#respon_demo').html(response);
				
					
					//Delete odl image if no click save button
					if( $('#product_image_url').val() ){
						$.ajax({
							 type:"POST",
							 url:site_root_domain+"/index.php?site=postnews&act=process_postnews_product_delete", 
							 data:"product_image="+$('#product_image_url').val(),
						});	
					}
			
			}
		});
		
	});
	
//Validate postnews

function confirm_validate(url){
	var anwser = confirm(lang_alert_delete);
	
	if( anwser ){
		window.location.href =url;
		
	}
} 

	
	
	